﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YunSpace.Application.Email;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.Entity.WorkFlow;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Domain.IRepository.WorkFlow;
using YunSpace.Domain.ViewModel.WorkFlow;
using YunSpace.Repository.OrderFlowManage;
using YunSpace.Repository.WorkFlow;

namespace YunSpace.Application.WorkFlow
{
    public partial class FlowManageApp
    {
        // private ICheckRecordRepository recordService = new CheckRecordRepository();
        // private IMessageNoticeRepository noticeService = new MessageNoticeRepository();

        /// <summary>
        /// 1. 根据流程编号获取相关流程信息
        /// </summary>
        private Dictionary<int, FlowDetailDto> GetFlowDetailByFormId(string flowFormId)
        {
            var result = new Dictionary<int, FlowDetailDto>();

            var sqlStr = $@"SELECT   WFD.F_Id,
		                             WFD.F_WorkFlowId,
		                             WFD.F_WorkFlowNo,
		                             WFD.F_StepName,
		                             WFD.F_TimeOutTip,
		                             WFD.F_IsFishTime,
		                             WFD.F_WorkHours, 
		                             WFD.F_TimeOutOpreation, 
		                             WFD.F_OpreationType, 
		                             WFD.F_OpreationUserId, 
		                             WFD.F_OpreationStrategy, 
		                             WFD.F_ReturnStrategy, 
		                             WFD.F_ReturnType, 
		                             WFD.F_MessageSend, 
		                             WFD.F_CcUserId,
                                     WFD.F_OperationPersonType,
                                     WFD.F_CcOperationPType,
                                     WFD.F_IsCountersign,
                                     WFD.F_CounTersignType,
                                     WFD.F_ConunTerisnUserId
		                    FROM Sys_WorkFlow WF
		                    INNER JOIN Sys_WorkFlowDetail WFD ON WF.F_Id = WFD.F_WorkFlowId
		                    WHERE WF.F_WorkFormId = @formId
		                    ORDER BY WFD.F_WorkFlowNo";
            using (var db = new RepositoryBase())
            {
                var dbParams = new MySqlParameter[] {
                    new MySqlParameter("@formId", flowFormId)
                };
                var data = db.FindList<FlowDetailDto>(sqlStr, dbParams);

                data.ForEach(item =>
                {
                    if (item.F_WorkFlowNo.HasValue && !result.ContainsKey(item.F_WorkFlowNo.Value))
                        result.Add(item.F_WorkFlowNo.Value, item);
                });
            }

            return result;
        }

        /// <summary>
        /// 2. 执行流程流转
        /// </summary>
        private void ExecuteFlow(string orderId, string workFlowId, string randomNo, FlowDetailDto flowDto)
        {
            // F_StepName   步骤名称
            // F_WorkFlowNo 流程序号
            // F_TimeOutTip 超期提示0:不提示，1：提示
            // F_IsFishTime 指定完成时间0:不指定，1：指定
            // F_WorkHours  工时(小时)
            // F_TimeOutOpreation   超时处理0:不处理，1：处理
            // F_OpreationType      处理方式0：邮件，1：短信，2：站内通知
            // F_OpreationUserId    处理人编号
            // F_OpreationStrategy  处理策略0:所有人必须同意，1：一人同意即可
            // F_ReturnStrategy     退回策略0：一人退回全部退回，1：所有人退回全部退回
            // F_ReturnType         退回类型0：退回前一步，1：退回到初试状态
            // F_MessageSend        消息发送方式0：邮件，1：短信，2：站内通知
            // F_CcUserId           抄送人编号
            var appTypes = new Dictionary<int?, string>{
                { 1, "场地档期审核" },
                { 2, "场地带看申请" },
                { 3, "场地意向报价" },
                { 4, "价格审核" },
                { 5, "场地预定申请" },
                { 6, "用印申请" },
                { 7, "合同审核" },
                { 8, "需求单完成" }
            };

            // 如果有相关申请人
            if (!flowDto.F_OpreationUserId.IsNullOrEmpty())
            {
                // 当前人信息
                var currentUser = OperatorProvider.Provider.GetCurrent();
                var opUserIds = flowDto.F_OpreationUserId.Split(',');

                using (var db = new RepositoryBase().BeginTrans())
                {
                    var workFlow = db.FindEntity<WorkFlowEntity>(p => p.F_WorkFormId == workFlowId);
                    var Order = db.FindEntity<ClientOrderEntity>(p => p.F_Id == orderId);
                    var Demand = db.FindEntity<SiteClientsProjectByDemandEntity>(t => t.F_Id == Order.F_DemandId);

                    //发送人集合
                    List<Tuple<string, string, string, string, string, string>> Send = new List<Tuple<string, string, string, string, string, string>>();
                    //抄送人集合
                    List<Tuple<string, string, string, string, string, string>> CC = new List<Tuple<string, string, string, string, string, string>>();

                    // 1.1 批量生成流程流转记录
                    foreach (var opUserId in opUserIds)
                    {
                        if (!opUserId.IsNullOrEmpty())
                        {
                            var CheckUserIdArr = GetCheckUserId(flowDto.F_WorkFlowNo ?? 0, flowDto.F_OperationPersonType, db, opUserId, orderId, randomNo);
                            Send.AddRange(CheckUserIdArr);
                            var UserIds = CheckUserIdArr.Select(t => t.Item1).Distinct();
                            foreach (var CheckUserId in UserIds)
                            {
                                var crEntity = new CheckRecordEntity
                                {
                                    F_FlowId = workFlow.F_Id,
                                    F_No = randomNo,
                                    F_OrderId = orderId,
                                    F_ApplyUserId = currentUser.UserId,
                                    F_ApplyType = flowDto.F_WorkFlowNo,
                                    F_CheckContent = $"{ flowDto.F_StepName }-{ appTypes[flowDto.F_WorkFlowNo] }",
                                    F_CheckUserId = CheckUserId,
                                    F_OrderState = Convert.ToInt32(flowDto.F_WorkFlowNo)
                                };
                                crEntity.Create();
                                db.Insert<CheckRecordEntity>(crEntity);

                                // 1.2.1 批量生成流程通知(审核通知)
                                var noticeEntity = new MessageNoticeEntity
                                {
                                    F_MessageWay = 1,   /*消息类型1：个人通知，2，全部人通知*/
                                    F_ReceiveUserId = CheckUserId,
                                    F_MessageType = 1,  /*消息类型1:审核通知，2：申请通知*/
                                    F_MessageContent = $"{ flowDto.F_StepName }-{ appTypes[flowDto.F_WorkFlowNo] }",
                                    F_IsRead = 0        /*是否已读0：未读，1：已读*/
                                };
                                noticeEntity.Create();
                                db.Insert<MessageNoticeEntity>(noticeEntity);
                            }
                        }
                    }

                    // 1.2.2 批量生成流程通知(申请通知)
                    if (!flowDto.F_CcUserId.IsNullOrEmpty())
                    {
                        var opCcIds = flowDto.F_CcUserId.Split(',');
                        foreach (var opCcId in opCcIds)
                        {
                            var ccUserIdArr = GetCCUserId(flowDto.F_WorkFlowNo ?? 0, flowDto.F_CcOperationPType, db, opCcId, orderId, randomNo);
                            CC.AddRange(ccUserIdArr);
                            var CcUserIds = ccUserIdArr.Select(t => t.Item1).Distinct();
                            foreach (var ccUserId in CcUserIds)
                            {
                                if (!opCcId.IsNullOrEmpty())
                                {
                                    var noticeEntity = new MessageNoticeEntity
                                    {
                                        F_MessageWay = 1,   /*消息类型1：个人通知，2，全部人通知*/
                                        F_ReceiveUserId = ccUserId,
                                        F_MessageType = 2,  /*消息类型1:审核通知，2：申请通知*/
                                        F_MessageContent = $"{ flowDto.F_StepName }-{ appTypes[flowDto.F_WorkFlowNo] }",
                                        F_IsRead = 0        /*是否已读0：未读，1：已读*/
                                    };
                                    noticeEntity.Create();
                                    db.Insert<MessageNoticeEntity>(noticeEntity);

                                }
                            }
                        }
                    }

                    SendToApply(Send, CC, $"{ flowDto.F_StepName }-{ appTypes[flowDto.F_WorkFlowNo] }");
                    db.Commit();
                }
            }
        }

        /// <summary>
        /// 3. 根据流程编号生成相关的流程信息
        /// </summary>
        /// <param name="CreateFlowInput">创建流程</param>
        public void CreateFlowRecord(CreateFlowInput input)
        {
            // 所有流程
            var allFlow = GetFlowDetailByFormId(input.FlowFormId);

            if (allFlow.ContainsKey(input.FlowState))
            {
                var orderId = input.OrderId;
                var flowId = input.FlowFormId;
                var no = input.RandomNo;
                var orderState = input.FlowState;
                var flow = allFlow[input.FlowState];

                // 执行流程
                ExecuteFlow(orderId, flowId, no, flow);
            }
            else
            {
                // Log 没有找到相关的流程
            }
        }

        /// <summary>
        /// 发送通知信息
        /// </summary>
        /// <param name="Send"></param>
        /// <param name="CC"></param>
        /// 审核人ID,审核人姓名,审核人邮箱，审核人手机,场地名称,部门ID
        private void SendToApply(List<Tuple<string, string, string, string, string, string>> Send, List<Tuple<string, string, string, string, string, string>> CC, string WorkFlowName)
        {
            //场地名称,收件人邮箱地址,抄送人邮箱地址,手机号
            List<Tuple<string, List<string>, List<string>, List<string>>> list = new List<Tuple<string, List<string>, List<string>, List<string>>>();
            foreach (var item in Send)
            {
                var CCList = CC.FindAll(t => t.Item5 == item.Item5);
                var obj = list.Find(t => t.Item1 == item.Item5);
                if (obj == null)
                {
                    var Phone = new List<string>() { item.Item4 };
                    Phone.AddRange(CCList.Select(t => t.Item4));
                    list.Add(new Tuple<string, List<string>, List<string>, List<string>>(item.Item5, new List<string>() { item.Item3 }, CCList.Select(t => t.Item3).ToList(), Phone));
                }
                else
                {
                    obj.Item2.Add(item.Item3);
                    obj.Item3.AddRange(CCList.Select(t => t.Item3).ToList());
                    obj.Item4.AddRange(CCList.Select(t => t.Item4).ToList());
                }
            }
            var ShangHu = Configs.GetValue("MailMerchants");
            var YinXiao = Configs.GetValue("MailSales");
            var FaZhan = Configs.GetValue("MailSms");
            foreach (var item in list)
            {
                item.Item3.Add(ShangHu);
                item.Item3.Add(YinXiao);
                item.Item3.Add(FaZhan);
                SendMessage.SendMessageToApply(item.Item1, WorkFlowName, item.Item2, item.Item3, item.Item4);
            }
        }

        /// <summary>
        /// 获取审核人信息
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns>审核人ID,审核人姓名,审核人邮箱，审核人手机,场地名称</returns>
        public List<Tuple<string, string, string, string, string, string>> GetCheckUserId(int F_WorkFlowNo, string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            switch (F_WorkFlowNo)
            {
                case 1: return GetRecommend(F_OperationPersonType, db, opUserId, orderId, randomNo);
                case 2: return GetTakelook(F_OperationPersonType, db, opUserId, orderId, randomNo);
                case 3: return GetPriceTalks(F_OperationPersonType, db, opUserId, orderId, randomNo);
                case 4: return GetPriceTalksAgree(F_OperationPersonType, db, opUserId, orderId, randomNo);
                case 5: return GetReserve(F_OperationPersonType, db, opUserId, orderId, randomNo);
                case 6: return GetUsing(F_OperationPersonType, db, opUserId, orderId, randomNo);
                case 7: return GetBookRecord(F_OperationPersonType, db, opUserId, orderId, randomNo);
                default: return new List<Tuple<string, string, string, string, string, string>>() { new Tuple<string, string, string, string, string, string>(opUserId, "", "", "", "", "") };
            }
        }

        /// <summary>
        /// 场地推荐
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns>审核人ID,审核人姓名,审核人邮箱，审核人手机,场地名称</returns>
        public List<Tuple<string, string, string, string, string, string>> GetRecommend(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();

            var sqlStr = $@"SELECT * FROM Site_Client_OrderRecommend WHERE F_OrderId='{ orderId }' AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrderrecommendEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_SiteId);
                foreach (var SiteId in existIds)
                {
                    var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<String, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));

                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }


        /// <summary>
        /// 场地带看
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetTakelook(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();

            var sqlStr = $@"SELECT * FROM Site_Client_OrderTakelook WHERE F_OrderId='{ orderId }' AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrdertakelookEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_SiteId);
                foreach (var SiteId in existIds)
                {
                    var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<String, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));

                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }

        /// <summary>
        /// 场地报价
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetPriceTalks(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();
            var sqlStr = $@"SELECT * FROM Site_Client_OrderIntention WHERE F_OrderId='{ orderId }' AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrderintentionEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_SiteId);
                foreach (var SiteId in existIds)
                {
                    var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<String, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));

                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }

        /// <summary>
        /// 价格审核
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetPriceTalksAgree(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();
            var sqlStr = $@"SELECT * FROM Site_Client_OrderIntention WHERE F_OrderId='{ orderId }' AND F_State=1 AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrderintentionEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_SiteId);
                foreach (var SiteId in existIds)
                {
                    var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<String, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));

                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }

        /// <summary>
        /// 场地预定
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetReserve(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();


            var sqlStr = $@"SELECT * FROM Site_Client_OrderReserve WHERE F_OrderId='{ orderId }' AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrderreserveEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_SiteId);
                foreach (var SiteId in existIds)
                {
                    var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<String, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));

                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }

        /// <summary>
        /// 签约记录
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetBookRecord(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();

            var sqlStr = $@"SELECT * FROM Site_Client_OrderSignContract WHERE F_OrderId='{ orderId }' AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrdersigncontractEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_SiteId);
                foreach (var SiteId in existIds)
                {
                    var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<String, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, Site.title, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }

        /// <summary>
        /// 用印申请
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="opUserId"></param>
        /// <param name="orderId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetUsing(string F_OperationPersonType, IRepositoryBase db, string opUserId, string orderId, string randomNo)
        {
            var CheckUserIds = new List<Tuple<string, string, string, string, string, string>>();

            var sqlStr = $@"SELECT * FROM Site_Client_OrderUsing WHERE F_OrderId='{ orderId }' AND F_No='{randomNo}'";
            var Sites = db.FindList<ClientOrderusingEntity>(sqlStr);
            if (Sites.Any())
            {
                var existIds = Sites.Select(p => p.F_FeeName);
                foreach (var SiteId in existIds)
                {
                    //var Site = db.FindEntity<SiteEntity>(t => t.F_Id == SiteId);
                    if (F_OperationPersonType == "2")
                    {
                        if (opUserId == "3a9dbd79-f716-4902-a21a-5b07e126b3c6")
                        {
                            //var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                            //CheckUserIds.Add(new Tuple<String, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, SiteId));
                        }
                        else
                        {
                            var User = db.FindEntity<UserEntity>(t => t.F_RoleId == opUserId && t.F_EnabledMark == true);
                            if (User != null)
                                CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, SiteId, User.F_DepartmentId));
                        }
                    }
                    else
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == opUserId && t.F_EnabledMark == true);
                        if (User != null)
                            CheckUserIds.Add(new Tuple<string, string, string, string, string, string>(User.F_Id, User.F_RealName, User.F_Email, User.F_MobilePhone, SiteId, User.F_DepartmentId));
                    }
                }
            }
            return CheckUserIds;
        }

        /// <summary>
        /// 获取抄送人ID
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="ccUserId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetCCUserId(int F_WorkFlowNo, string F_OperationPersonType, IRepositoryBase db, string ccUserId, string orderId, string randomNo)
        {
            var CcUserIds = new List<Tuple<string, string, string, string, string, string>>();
            if (F_OperationPersonType == "2")
            {
                if (ccUserId == "2907e276-cde6-4233-ae6c-5172f5ab15c4")
                {
                    var opUserId = "3a9dbd79-f716-4902-a21a-5b07e126b3c6";
                    var list = GetCheckUserId(F_WorkFlowNo, F_OperationPersonType, db, opUserId, orderId, randomNo);
                    foreach (var CheckUserId in list)
                    {
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == CheckUserId.Item1 && t.F_EnabledMark == true);
                        var Department = User.F_DepartmentId;
                        var Organize = db.FindEntity<OrganizeEntity>(t => t.F_Id == Department);
                        var managerId = Organize.F_ManagerId.Split(',');
                        foreach (var item in managerId)
                        {
                            var UserEntity = db.FindEntity<UserEntity>(t => item == t.F_Id);
                            if (UserEntity != null)
                                CcUserIds.Add(new Tuple<string, string, string, string, string, string>(UserEntity.F_Id, UserEntity.F_RealName, UserEntity.F_Email, UserEntity.F_MobilePhone, CheckUserId.Item5, UserEntity.F_DepartmentId));
                        }
                    }
                }
                return CcUserIds;
            }
            return GetCheckUserId(F_WorkFlowNo, F_OperationPersonType, db, ccUserId, orderId, randomNo);
        }

        /// <summary>
        /// 获取抄送人ID
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="db"></param>
        /// <param name="ccUserId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Tuple<string, string, string, string, string, string>> GetUserId(int F_WorkFlowNo, string F_OperationPersonType, IRepositoryBase db, string ccUserId, string orderId, string randomNo, string SiteIds)
        {
            var CcUserIds = new List<Tuple<string, string, string, string, string, string>>();
            if (F_OperationPersonType == "2")
            {
                if (ccUserId == "2907e276-cde6-4233-ae6c-5172f5ab15c4")
                {
                    var SiteIdArr = SiteIds.Split(',');
                    foreach (var item in SiteIdArr)
                    {
                        var Site = db.FindEntity<SiteEntity>(item);
                        var User = db.FindEntity<UserEntity>(t => t.F_Id == Site.Mocha_ITOM && t.F_EnabledMark == true);
                        var Department = User.F_DepartmentId;
                        var Organize = db.FindEntity<OrganizeEntity>(t => t.F_Id == Department);
                        var managerId = Organize.F_ManagerId.Split(',');
                        foreach (var it in managerId)
                        {
                            var UserEntity = db.FindEntity<UserEntity>(t => it == t.F_Id);
                            if (UserEntity != null)
                                CcUserIds.Add(new Tuple<string, string, string, string, string, string>(UserEntity.F_Id, UserEntity.F_RealName, UserEntity.F_Email, UserEntity.F_MobilePhone, Site.title, UserEntity.F_DepartmentId));
                        }
                    }
                    return CcUserIds;
                }
            }
            return GetCheckUserId(F_WorkFlowNo, F_OperationPersonType, db, ccUserId, orderId, randomNo);
        }
    }
}
